package com.resume.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class ImageUtil {
    /**
     * 图片按比率缩放
     *
     * @param src  源图片
     * @param dest 生成的图片
     * @param rate 缩放比率
     */
    public static void scale(String src, String dest, double rate) {
        File srcImg = new File(src);
        if (!srcImg.exists()) {//如果文件不存在返回
            return;
        }
        File destImg = new File(dest);

        String type = FileUtil.getFileSuffix(src);//获取文件后缀
        if (type == null) {
            return;
        }

        BufferedImage bufImg = null;
        try {
            bufImg = ImageIO.read(srcImg);
            System.out.println("srcImg size=" + bufImg.getWidth() + "X" + bufImg.getHeight());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("destImg size=" + (int) (bufImg.getWidth() * rate) + "X" + (int) (bufImg.getHeight() * rate));

        Image temp = bufImg.getScaledInstance(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.SCALE_SMOOTH);
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(rate, rate), null);
        temp = ato.filter(bufImg, null);
        try {
            ImageIO.write((BufferedImage) temp, type, destImg);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @param src    原图片
     * @param dest   目标位置
     * @param width  期望宽
     * @param height 期望高
     * @param flag   是否等比例缩放
     */
    public static void scale(String src, String dest, int width, int height, boolean flag) {
        File srcImg = new File(src);
        if (!srcImg.exists()) {//如果文件不存在返回
            return;
        }
        String type = FileUtil.getFileSuffix(src);//获取文件后缀
        if (type == null) {
            return;
        }
        if (width < 0 || height < 0) {
            return;
        }

        BufferedImage bufImg = null;
        try {
            bufImg = ImageIO.read(srcImg);
            System.out.println("srcImg size=" + bufImg.getWidth() + "X" + bufImg.getHeight());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        if (bufImg != null) {
            // targetW，targetH分别表示目标长和宽
            BufferedImage target = null;
            double sx = (double) width / bufImg.getWidth();
            double sy = (double) height / bufImg.getHeight();
            // 等比缩放
            if (flag) {
                if (sx > sy) {
                    sx = sy;
                    width = (int) (sx * bufImg.getWidth());
                } else {
                    sy = sx;
                    height = (int) (sy * bufImg.getHeight());
                }
            }
            System.out.println("destImg size=" + width + "X" + height);
            ColorModel cm = bufImg.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();

            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
            Graphics2D g = target.createGraphics();
            // smoother than exlax:
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g.drawRenderedImage(bufImg, AffineTransform.getScaleInstance(sx, sy));
            g.dispose();
            // 将转换后的图片保存
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(target, type, baos);
                File destImg = new File(dest);
                FileOutputStream fos = new FileOutputStream(destImg);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}